﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class SysManage_RolesM_BWebRolesAndUserRight : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);
        if (!IsPostBack)
        {
            UserSysRight(Session["UserCode"].ToString());
        }
    }

    #region 初始化绑定 UserSysRight(string UserCode)
    protected void UserSysRight(string UserCode)
    {
        //权限
        comHelp.GetRightUnite(this, new string[] { UserCode, Session["CoCode"].ToString(), "webRolesAndUserRight" }, 
            new string[] { "Button1", "Button2", "Button3", "Button4" }, new int[] { 1, 3, 1, 3 }, CommonModel.loginOut1);
        //绑定用户组
        UserGroupBind();
        UserListBind(ddlUserGroupName.SelectedValue.ToString());
        //绑定页面字段角色组
        GroupBind2();
        ListBind2(ddlWebRoleGroupName.SelectedValue.ToString());
        //权限绑定
        RightBind(ddlWebRoleGroupName.SelectedValue.ToString(), ddlUserGroupName.SelectedValue.ToString());
    }

    //绑定页面角色组
    protected void GroupBind2()
    {
        string StrWebGroupSql = string.Format(@" select RG.RGCode,RG.RGName from B_RolesGroup RG where RG.DeleteFlag<>1");
        DataTable dt = help.GetDataTable(StrWebGroupSql);
        DataTable dt2 = help.GetDataTable(string.Format(@" select	distinct UGCode, RGCode, RightType, RightName, DeleteFlag
                    from	B_RolesAndUserRight
                    where	UGCode='{0}' and DeleteFlag<>1 ", ddlUserGroupName.SelectedValue));
        int n = dt.Rows.Count;
        DataRow[] rows = null;
        string temp = string.Empty;
        for (int i = 0; i < n; i++)
        {
            temp = dt.Rows[i]["RGName"].ToString() + "  (权限-Right:";
            rows = dt2.Select(string.Format("RGCode='{0}'", dt.Rows[i]["RGCode"].ToString()));
            foreach (DataRow row in rows)
            {
                temp += row["RightName"].ToString() + "|";
            }
            dt.Rows[i]["RGName"] = (temp + ")").Replace("|)", ")");
        }
        ddlWebRoleGroupName.DataTextField = "RGName";
        ddlWebRoleGroupName.DataValueField = "RGCode";
        ddlWebRoleGroupName.DataSource = dt;
        ddlWebRoleGroupName.DataBind();
    }
    //组的页面字段
    protected void ListBind2(string code)
    {  
        //清空
        lbxWebRloesName.Items.Clear();
        string StrSql = string.Format("select RC.ColumnType,RC.ColumnName from B_Roles_Column RC join "
            + " B_RolesGroupAndColumn BGC on RC.ColumnType=BGC.ColumnType where BGC.RGCode='{0}' order by RC.ColumnPosition", code);
        lbxWebRloesName.DataSource = help.GetDataTable(StrSql);
        lbxWebRloesName.DataValueField = "ColumnType";
        lbxWebRloesName.DataTextField = "ColumnName";
        lbxWebRloesName.DataBind();
    }
    //绑定用户组
    protected void UserGroupBind()
    {
        string StrUserGroupeSql = " select UGCode,UGName from B_UserGroup where DeleteFlag<>1 ";
        ddlUserGroupName.DataTextField = "UGName";
        ddlUserGroupName.DataValueField = "UGCode";
        ddlUserGroupName.DataSource = help.GetDataTable(StrUserGroupeSql);
        ddlUserGroupName.DataBind();
    }
    //组的用户
    protected void UserListBind(string UGCode)
    {   //清空
        lbxUserName.Items.Clear();
        string StrSql = string.Format("select U.UserCode,U.UserEngName+'-'+U.UserFullName UserName from B_User U join B_UserAndGroup UG "
            + " on U.UserCode=UG.UserCode where UG.UGCode='{0}' order by U.UserEngName ", UGCode);
        lbxUserName.DataSource = help.GetDataTable(StrSql);
        lbxUserName.DataValueField = "UserCode";
        lbxUserName.DataTextField = "UserName";
        lbxUserName.DataBind();
    }
    #endregion

    //分别绑定未在此页面字段角色组和用户组的权限的列表
    protected void RightBind(string RGCode, string UGCode)
    {
        //未在组里的
        for (int i = 0; i < lbxInRightName.Items.Count; i++)
        { lbxNotInRightName.Items.Add(lbxInRightName.Items[i]); }
        //在组里的
        string StrWNGSql = " select RightType,RightName from B_RolesAndUserRight where UGCode='"
            + UGCode + "' and RGCode='" + RGCode + "'";
        DataTable Dt = help.GetDataTable(StrWNGSql);
        lbxInRightName.DataSource = Dt;
        lbxInRightName.DataValueField = "RightType";
        lbxInRightName.DataTextField = "RightName";
        lbxInRightName.DataBind();

        lbxInsert2.DataSource = Dt;
        lbxInsert2.DataValueField = "RightType";
        lbxInsert2.DataTextField = "RightName";
        lbxInsert2.DataBind();

        //未在组里的
        for (int i = 0; i < lbxInRightName.Items.Count; i++)
        { lbxNotInRightName.Items.Remove(lbxInRightName.Items[i]); }
    }

    //根据页面字段角色组动态绑定页面列表
    protected void ddlWebRoleGroupName_TextChanged(object sender, EventArgs e)
    {
        LbxClear();
        ListBind2(ddlWebRoleGroupName.SelectedValue.ToString());
        RightBind(ddlWebRoleGroupName.SelectedValue.ToString(),
            ddlUserGroupName.SelectedValue.ToString());
    }
    //根据用户组动态绑定用户列表
    protected void ddlUserGroupName_TextChanged(object sender, EventArgs e)
    {
        LbxClear();
        UserListBind(ddlUserGroupName.SelectedValue.ToString());
        RightBind(ddlWebRoleGroupName.SelectedValue.ToString(),
            ddlUserGroupName.SelectedValue.ToString());
        GroupBind2();
    }

    #region 页面组与用户组权限分配
    //单项添加
    protected void Button1_Click(object sender, EventArgs e)
    {
        //不在组里的页面数
        for (int i = 0; i < lbxNotInRightName.Items.Count; i++)
        {
            if (lbxNotInRightName.SelectedIndex >= 0)
            {
                lbxInRightName.Items.Add(lbxNotInRightName.SelectedItem);

                //是否插入
                int INum = 0;
                string StrUserName = lbxNotInRightName.SelectedItem.Text.ToString();

                for (int d = 0; d < lbxInsert2.Items.Count; d++)
                {
                    if (StrUserName.Equals(lbxInsert2.Items[d].Text.ToString()))
                    { ++INum; }
                }

                if (INum < 1)
                { lbxInsert.Items.Add(lbxNotInRightName.SelectedItem); }

                lbxNotInRightName.Items.Remove(lbxNotInRightName.SelectedItem);
                lbxDelete.Items.Remove(lbxNotInRightName.SelectedItem);
            }
        }
    }
    //单项移除
    protected void Button2_Click(object sender, EventArgs e)
    {
        //不在组里的
        for (int i = 0; i < lbxInRightName.Items.Count; i++)
        {
            if (lbxInRightName.SelectedIndex >= 0)
            {
                lbxNotInRightName.Items.Add(lbxInRightName.SelectedItem);
                //插入行
                int INum = 0;
                //页面类型
                string WebTypeStr = lbxInRightName.SelectedItem.Text.ToString();

                for (int k = 0; k < lbxInsert2.Items.Count; k++)
                {
                    if (WebTypeStr.Equals(lbxInsert2.Items[k].Text.ToString()))
                    {
                        ++INum;
                    }
                }
                if (INum > 0)
                {
                    lbxDelete.Items.Add(lbxInRightName.SelectedItem);
                }
                lbxInsert.Items.Remove(lbxInRightName.SelectedItem);
                lbxInRightName.Items.Remove(lbxInRightName.SelectedItem);
            }
        }
    }
    //全部添加
    protected void Button4_Click(object sender, EventArgs e)
    {
        //在删除组的
        for (int j = 0; j < lbxDelete.Items.Count; j++)
        {
            lbxNotInRightName.Items.Remove(lbxDelete.Items[j]);
            lbxInRightName.Items.Add(lbxDelete.Items[j]);
        }
        //不在组里的
        for (int i = 0; i < lbxNotInRightName.Items.Count; i++)
        {
            lbxInRightName.Items.Add(lbxNotInRightName.Items[i]);
            lbxInsert.Items.Add(lbxNotInRightName.Items[i]);
        }
        lbxNotInRightName.Items.Clear();
        lbxDelete.Items.Clear();
    }
    //全部删除
    protected void Button3_Click(object sender, EventArgs e)
    {
        //在添加组的
        for (int k = 0; k < lbxInsert.Items.Count; k++)
        {
            lbxNotInRightName.Items.Add(lbxInsert.Items[k]);
            lbxInRightName.Items.Remove(lbxInsert.Items[k]);
        }
        //不在组里的
        for (int i = 0; i < lbxInRightName.Items.Count; i++)
        {
            lbxNotInRightName.Items.Add(lbxInRightName.Items[i]);
            lbxDelete.Items.Add(lbxInRightName.Items[i]);
        }
        lbxInRightName.Items.Clear();
        lbxInsert.Items.Clear();
    }
    #endregion

    //保存
    protected void btnSave_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);
        string Str = string.Empty;//操作字符串
        //页面字段角色组编号
        string RGCode = ddlWebRoleGroupName.SelectedValue.ToString();
        //用户组编号
        string UGCode = ddlUserGroupName.SelectedValue.ToString();

        //当前用户
        string UserName = Session["UserCode"].ToString();
        //当前日期
        string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");

        //统计插入与删除的行数
        int InsertN = lbxInsert.Items.Count;
        int DeleteN = lbxDelete.Items.Count;

        //删除
        if (DeleteN > 0)
        {   //删除SQL语句前部分
            string DeleteStr = " Delete from B_RolesAndUserRight where RGCode='"
                + RGCode + "' and UGCode='" + UGCode + "' and RightType='";
            for (int j = 0; j < DeleteN; j++)
            { Str += DeleteStr + lbxDelete.Items[j].Value.ToString() + "';   "; }
        }//插入
        if (InsertN > 0)
        {   //插入SQL语句前部分
            string InsertStr = " Insert Into B_RolesAndUserRight(DeleteFlag,AddAuthor,"
                + "AddDate,UpdateAuthor,UpdateDate,RGCode,UGCode,RightType,RightName) values('0','" + UserName
                + "','" + StrDate + "','" + UserName + "','" + StrDate + "','" + RGCode + "','" + UGCode + "','";

            for (int i = 0; i < InsertN; i++)
            {
                Str += InsertStr + lbxInsert.Items[i].Value.ToString()
                    + "','" + lbxInsert.Items[i].Text.ToString() + "');  ";
            }
        }

        if (!string.IsNullOrEmpty(Str))
        {
            if (help.RunSqlInt(Str) > 0)
            {
                CommonMethod.regAlertMessage("操作成功！", this);
                RightBind(ddlWebRoleGroupName.SelectedItem.Value.ToString(),
                    ddlUserGroupName.SelectedValue.ToString());
                LbxClear();
            }
            else { CommonMethod.regAlertMessage("操作失败！", this); }
        }
        else { CommonMethod.regAlertMessage("选择项没有变动！", this); }
    }
    //清空
    protected void LbxClear()
    {
        lbxDelete.Items.Clear();
        lbxInsert.Items.Clear();
        lbxInsert2.Items.Clear();
    }
    protected void lbxWebRloesName_DataBound(object sender, EventArgs e)
    {

    }
}